<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="A very lame cool person">
    <meta name="keyword" content="null">
    <meta name="theme-color" content="#600090">
    <meta name="msapplication-navbutton-color" content="#600090">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="#600090">
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="alternate" type="application/atom+xml" title="Guanghui" href="/atom.xml">
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.css">
    <title>
        
        Asp.net MVC系列教程3——公共基础数据操作类｜Guanghui&#39;s blog
        
    </title>

    <link rel="canonical" href="https://wghglory.github.io/2017/02/16/mvc-lesson3/">

    <!-- Bootstrap Core CSS -->
    <link rel="stylesheet" href="/css/bootstrap.min.css">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/blog-style.css">

    <link rel="stylesheet" href="/css/my.css">

    <!-- Pygments Github CSS -->
    <link rel="stylesheet" href="/css/syntax.css">
</head>

<style>

    header.intro-header {
        background-image: url('/images/tag.jpg')
    }
</style>
<!-- hack iOS CSS :active style -->
<body ontouchstart="" class="animated fadeIn">
<!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top " id="nav-top" data-ispost = "true" data-istags="false
" data-ishome = "false" >
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand animated pulse" href="/">
                <span class="brand-logo">
                    Guanghui
                </span>
                's Blog
            </a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <!-- Known Issue, found by Hux:
            <nav>'s height woule be hold on by its content.
            so, when navbar scale out, the <nav> will cover tags.
            also mask any touch event of tags, unfortunately.
        -->
        <!-- /.navbar-collapse -->
        <div id="huxblog_navbar">
            <div class="navbar-collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="/">Home</a>
                    </li>
					
                    
                        
							
                        <li>
                            <a href="/tags/">tags</a>
                        </li>
							
						
                    
                        
							
                        <li>
                            <a href="/works/">my works</a>
                        </li>
							
						
                    

					
                        <li>
                            <a href="https://wghglory.github.io/guanghui.resume/" target="_blank">Resume</a>
                        </li>
                </ul>
            </div>
        </div>
    </div>
    <!-- /.container -->
</nav>
<script>
    // Drop Bootstarp low-performance Navbar
    // Use customize navbar with high-quality material design animation
    // in high-perf jank-free CSS3 implementation
//    var $body   = document.body;
    var $toggle = document.querySelector('.navbar-toggle');
    var $navbar = document.querySelector('#huxblog_navbar');
    var $collapse = document.querySelector('.navbar-collapse');

    $toggle.addEventListener('click', handleMagic)
    function handleMagic(e){
        if ($navbar.className.indexOf('in') > 0) {
        // CLOSE
            $navbar.className = " ";
            // wait until animation end.
            setTimeout(function(){
                // prevent frequently toggle
                if($navbar.className.indexOf('in') < 0) {
                    $collapse.style.height = "0px"
                }
            },400)
        }else{
        // OPEN
            $collapse.style.height = "auto"
            $navbar.className += " in";
        }
    }
</script>

<!-- Main Content -->

<!--only post-->


<img class="wechat-title-img" src="/images/default-post.jpg">


<style>
    
    header.intro-header {
        background-image: url('/images/default-post.jpg');
    }

    
</style>

<header class="intro-header">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1 text-center">
                <div class="post-heading">
                    <h1>Asp.net MVC系列教程3——公共基础数据操作类</h1>
                    
                    <span class="meta">
                         作者 Guanghui Wang
                        <span>
                          日期 2017-02-16
                         </span>
                    </span>
                    <div class="tags text-center">
                        
                        <a class="tag" href="/tags/#Asp.net"
                           title="Asp.net">Asp.net</a>
                        
                        <a class="tag" href="/tags/#MVC"
                           title="MVC">MVC</a>
                        
                        <a class="tag" href="/tags/#Repository"
                           title="Repository">Repository</a>
                        
                        <a class="tag" href="/tags/#EntityFramework"
                           title="EntityFramework">EntityFramework</a>
                        
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="post-title-haojen">
        <span>
            <!-- Asp.net MVC系列教程3——公共基础数据操作类 -->
        </span>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">
            <!-- Post Container -->
            <div class="col-lg-8 col-lg-offset-1 col-sm-9 post-container">
                <h1 id="简述"><a href="#简述" class="headerlink" title="简述"></a>简述</h1><p>本文转自<a href="http://yuangang.cnblogs.com并加以整理。" target="_blank" rel="noopener">http://yuangang.cnblogs.com并加以整理。</a> 以下内容包括Common类库添加公共帮助类和Service类库对数据库操作接口、实现</p>
<h1 id="索引"><a href="#索引" class="headerlink" title="索引"></a>索引</h1><p><a href="/2017/02/14/mvc-lesson1/" target="_blank">Asp.net MVC项目系列教程</a></p>
<h1 id="项目开始"><a href="#项目开始" class="headerlink" title="项目开始"></a>项目开始</h1><h3 id="一、Common类库添加"><a href="#一、Common类库添加" class="headerlink" title="一、Common类库添加"></a>一、Common类库添加</h3><p>大家可以在网上搜<strong>C#公共帮助类</strong>，把他们添加到Common项目中去，注意添加好引用。<br><img src="/2017/02/16/mvc-lesson3/mvc3.png" style="height:500px;"></p>
<h3 id="二、Service项目添加IRepository，RepositoryBase，DatabaseExtensions"><a href="#二、Service项目添加IRepository，RepositoryBase，DatabaseExtensions" class="headerlink" title="二、Service项目添加IRepository，RepositoryBase，DatabaseExtensions"></a>二、Service项目添加IRepository，RepositoryBase，DatabaseExtensions</h3><ol>
<li><p>先在Service类库中添加对Domain和Common的项目引用，因为Service类似于数据服务管理层。</p>
</li>
<li><p>由于Repository和DatabaseExtensions会涉及到EF相关操作，所以要添加对EntityFramework和EntityFramework.sqlServer的引用，这里有一个tricky，是添加ADO.net数据模型，然后再删除它。</p>
</li>
<li><p>下面是IRepository代码，<strong>注意在分页查询中引用了Common类库中的<a href="https://gist.github.com/wghglory/7cb49c6529cc19c33a4b1e756f36edfd" target="_blank" rel="noopener">PageCollection.cs(查看)</a></strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> Common;</span><br><span class="line"><span class="keyword">using</span> System;</span><br><span class="line"><span class="keyword">using</span> System.Collections.Generic;</span><br><span class="line"><span class="keyword">using</span> System.Data.Common;</span><br><span class="line"><span class="keyword">using</span> System.Linq;</span><br><span class="line"><span class="keyword">using</span> System.Data.Entity.Infrastructure;</span><br><span class="line"><span class="keyword">using</span> System.Data.Entity;</span><br><span class="line"><span class="keyword">using</span> System.Linq.Expressions;</span><br><span class="line"><span class="keyword">using</span> Domain;</span><br><span class="line"><span class="keyword">using</span> System.Collections;</span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> Service</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">    <span class="comment">/// 所有的数据操作基类接口</span></span><br><span class="line">    <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">    <span class="keyword">public</span> interface IRepository&lt;T&gt; where T : <span class="class"><span class="keyword">class</span></span></span><br><span class="line"><span class="class">    &#123;</span></span><br><span class="line">        <span class="meta">#region 数据对象操作</span></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 数据上下文</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        DbContext Context &#123; get; &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 数据上下文</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        MyConfig Config &#123; get; &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 数据模型操作</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        DbSet&lt;T&gt; dbSet &#123; get; &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// EF事务</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        DbContextTransaction Transaction &#123; get; <span class="built_in">set</span>; &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 事务提交结果</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">bool</span> Committed &#123; get; <span class="built_in">set</span>; &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 提交事务</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">void</span> <span class="title">Commit</span><span class="params">()</span></span>;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 回滚事务</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">void</span> <span class="title">Rollback</span><span class="params">()</span></span>;</span><br><span class="line">        <span class="meta">#endregion</span></span><br><span class="line"></span><br><span class="line">        <span class="meta">#region 单模型操作</span></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 获取实体</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="predicate"&gt;&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;returns&gt;实体&lt;/returns&gt;</span></span><br><span class="line">        <span class="function">T <span class="title">Get</span><span class="params">(Expression&lt;Func&lt;T, <span class="keyword">bool</span>&gt;&gt; predicate)</span></span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 插入实体</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="entity"&gt;实体&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;returns&gt;&lt;/returns&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">bool</span> <span class="title">Save</span><span class="params">(T entity)</span></span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 修改实体</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="entity"&gt;实体&lt;/param&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">bool</span> <span class="title">Update</span><span class="params">(T entity)</span></span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 修改或保存实体</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="entity"&gt;实体&lt;/param&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">bool</span> <span class="title">SaveOrUpdate</span><span class="params">(T entity, <span class="keyword">bool</span> isEdit)</span></span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 删除实体</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">int</span> <span class="title">Delete</span><span class="params">(Expression&lt;Func&lt;T, <span class="keyword">bool</span>&gt;&gt; predicate = null)</span></span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 执行SQL删除</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">int</span> <span class="title">DeleteBySql</span><span class="params">(<span class="built_in">string</span> sql, params DbParameter[] para)</span></span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 根据属性验证实体对象是否存在</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">bool</span> <span class="title">IsExist</span><span class="params">(Expression&lt;Func&lt;T, <span class="keyword">bool</span>&gt;&gt; predicate)</span></span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 根据SQL验证实体对象是否存在</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">bool</span> <span class="title">IsExist</span><span class="params">(<span class="built_in">string</span> sql, params DbParameter[] para)</span></span>;</span><br><span class="line">        <span class="meta">#endregion</span></span><br><span class="line"></span><br><span class="line">        <span class="meta">#region 多模型操作</span></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 增加多模型数据，指定独立模型集合</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">int</span> SaveList&lt;T1&gt;(List&lt;T1&gt; t) where T1 : <span class="class"><span class="keyword">class</span>;</span></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 增加多模型数据，与当前模型一致</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">int</span> <span class="title">SaveList</span><span class="params">(List&lt;T&gt; t)</span></span>;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 更新多模型，指定独立模型集合</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">int</span> UpdateList&lt;T1&gt;(List&lt;T1&gt; t) where T1 : <span class="class"><span class="keyword">class</span>;</span></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 更新多模型，与当前模型一致</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">int</span> <span class="title">UpdateList</span><span class="params">(List&lt;T&gt; t)</span></span>;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 批量删除数据，当前模型</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">int</span> <span class="title">DeleteList</span><span class="params">(List&lt;T&gt; t)</span></span>;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 批量删除数据，独立模型</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">int</span> DeleteList&lt;T1&gt;(List&lt;T1&gt; t) where T1 : <span class="class"><span class="keyword">class</span>;</span></span><br><span class="line">        <span class="meta">#endregion</span></span><br><span class="line"></span><br><span class="line">        <span class="meta">#region 存储过程操作</span></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 执行增删改存储过程</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function">object <span class="title">ExecuteProc</span><span class="params">(<span class="built_in">string</span> procname, params DbParameter[] parameter)</span></span>;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 执行查询的存储过程</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function">object <span class="title">ExecuteQueryProc</span><span class="params">(<span class="built_in">string</span> procname, params DbParameter[] parameter)</span></span>;</span><br><span class="line">        <span class="meta">#endregion</span></span><br><span class="line"></span><br><span class="line">        <span class="meta">#region 查询多条数据</span></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 获取集合 IQueryable</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        IQueryable&lt;T&gt; LoadAll(Expression&lt;Func&lt;T, <span class="keyword">bool</span>&gt;&gt; predicate);</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 获取集合 IList</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        List&lt;T&gt; LoadListAll(Expression&lt;Func&lt;T, <span class="keyword">bool</span>&gt;&gt; predicate);</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 获取DbQuery的列表</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        DbQuery&lt;T&gt; LoadQueryAll(Expression&lt;Func&lt;T, <span class="keyword">bool</span>&gt;&gt; predicate);</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 获取IEnumerable列表</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        IEnumerable&lt;T&gt; LoadEnumerableAll(<span class="built_in">string</span> sql, params DbParameter[] para);</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 获取数据动态集合</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function">IEnumerable <span class="title">LoadEnumerable</span><span class="params">(<span class="built_in">string</span> sql, params DbParameter[] para)</span></span>;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 采用SQL进行数据的查询，并转换</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        List&lt;T&gt; SelectBySql(<span class="built_in">string</span> sql, params DbParameter[] para);</span><br><span class="line">        List&lt;T1&gt; SelectBySql&lt;T1&gt;(<span class="built_in">string</span> sql, params DbParameter[] para);</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 可指定返回结果、排序、查询条件的通用查询方法，返回实体对象</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;typeparam name="TEntity"&gt;实体对象&lt;/typeparam&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;typeparam name="TOrderBy"&gt;排序字段类型&lt;/typeparam&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;typeparam name="TResult"&gt;数据结果，一般为object&lt;/typeparam&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="where"&gt;过滤条件，需要用到类型转换的需要提前处理与数据表一致的&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="orderby"&gt;排序字段&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="selector"&gt;返回结果（必须是模型中存在的字段）&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="IsAsc"&gt;排序方向，true为正序false为倒序&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;returns&gt;实体集合&lt;/returns&gt;</span></span><br><span class="line">        List&lt;TResult&gt; QueryEntity&lt;TEntity, TOrderBy, TResult&gt;(Expression&lt;Func&lt;TEntity, <span class="keyword">bool</span>&gt;&gt; where, Expression&lt;Func&lt;TEntity, TOrderBy&gt;&gt; orderby, Expression&lt;Func&lt;TEntity, TResult&gt;&gt; selector, <span class="keyword">bool</span> IsAsc)</span><br><span class="line">            where TEntity : <span class="class"><span class="keyword">class</span></span></span><br><span class="line"><span class="class">            <span class="title">where</span> <span class="title">TResult</span> :</span> <span class="class"><span class="keyword">class</span>;</span></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 可指定返回结果、排序、查询条件的通用查询方法，返回Object对象</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;typeparam name="TEntity"&gt;实体对象&lt;/typeparam&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;typeparam name="TOrderBy"&gt;排序字段类型&lt;/typeparam&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="where"&gt;过滤条件，需要用到类型转换的需要提前处理与数据表一致的&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="orderby"&gt;排序字段&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="selector"&gt;返回结果（必须是模型中存在的字段）&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="IsAsc"&gt;排序方向，true为正序false为倒序&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;returns&gt;自定义实体集合&lt;/returns&gt;</span></span><br><span class="line">        List&lt;object&gt; QueryObject&lt;TEntity, TOrderBy&gt;(Expression&lt;Func&lt;TEntity, <span class="keyword">bool</span>&gt;&gt; where, Expression&lt;Func&lt;TEntity, TOrderBy&gt;&gt; orderby, Func&lt;IQueryable&lt;TEntity&gt;, List&lt;object&gt;&gt; selector, <span class="keyword">bool</span> IsAsc)</span><br><span class="line">            where TEntity : <span class="class"><span class="keyword">class</span>;</span></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 可指定返回结果、排序、查询条件的通用查询方法，返回动态类对象</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;typeparam name="TEntity"&gt;实体对象&lt;/typeparam&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;typeparam name="TOrderBy"&gt;排序字段类型&lt;/typeparam&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="where"&gt;过滤条件，需要用到类型转换的需要提前处理与数据表一致的&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="orderby"&gt;排序字段&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="selector"&gt;返回结果（必须是模型中存在的字段）&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="IsAsc"&gt;排序方向，true为正序false为倒序&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;returns&gt;动态类对象&lt;/returns&gt;</span></span><br><span class="line">        dynamic QueryDynamic&lt;TEntity, TOrderBy&gt;(Expression&lt;Func&lt;TEntity, <span class="keyword">bool</span>&gt;&gt; where, Expression&lt;Func&lt;TEntity, TOrderBy&gt;&gt; orderby, Func&lt;IQueryable&lt;TEntity&gt;, List&lt;object&gt;&gt; selector, <span class="keyword">bool</span> IsAsc)</span><br><span class="line">            where TEntity : <span class="class"><span class="keyword">class</span>;</span></span><br><span class="line">        <span class="meta">#endregion</span></span><br><span class="line"></span><br><span class="line">        <span class="meta">#region 分页查询</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 通过SQL分页</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="sql"&gt;&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="parameters"&gt;&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="page"&gt;&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;returns&gt;&lt;/returns&gt;</span></span><br><span class="line">        IList&lt;T1&gt; PageByListSql&lt;T1&gt;(<span class="built_in">string</span> sql, IList&lt;DbParameter&gt; parameters, PageCollection page);</span><br><span class="line">        IList&lt;T&gt; PageByListSql(<span class="built_in">string</span> sql, IList&lt;DbParameter&gt; parameters, PageCollection page);</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 通用EF分页，默认显示20条记录</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;typeparam name="TEntity"&gt;实体模型&lt;/typeparam&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;typeparam name="TOrderBy"&gt;排序类型&lt;/typeparam&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="index"&gt;当前页&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="pageSize"&gt;显示条数&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="where"&gt;过滤条件&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="orderby"&gt;排序字段&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="selector"&gt;结果集合&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="isAsc"&gt;排序方向true正序 false倒序&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;returns&gt;自定义实体集合&lt;/returns&gt;</span></span><br><span class="line">        PageInfo&lt;object&gt; Query&lt;TEntity, TOrderBy&gt;</span><br><span class="line">            (<span class="keyword">int</span> index, <span class="keyword">int</span> pageSize,</span><br><span class="line">            Expression&lt;Func&lt;TEntity, <span class="keyword">bool</span>&gt;&gt; where,</span><br><span class="line">            Expression&lt;Func&lt;TEntity, TOrderBy&gt;&gt; orderby,</span><br><span class="line">            Func&lt;IQueryable&lt;TEntity&gt;, List&lt;object&gt;&gt; selector,</span><br><span class="line">            <span class="keyword">bool</span> IsAsc)</span><br><span class="line">            where TEntity : <span class="class"><span class="keyword">class</span>;</span></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 对IQueryable对象进行分页逻辑处理，过滤、查询项、排序对IQueryable操作</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="t"&gt;Iqueryable&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="index"&gt;当前页&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="pageSize"&gt;每页显示多少条&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;returns&gt;当前IQueryable to List的对象&lt;/returns&gt;</span></span><br><span class="line">        Common.PageInfo&lt;T&gt; Query(IQueryable&lt;T&gt; query, <span class="keyword">int</span> index, <span class="keyword">int</span> pageSize);</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 普通SQL查询分页方法</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="index"&gt;当前页&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="pageSize"&gt;显示行数&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="tableName"&gt;表名/视图&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="field"&gt;获取项&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="filter"&gt;过滤条件&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="orderby"&gt;排序字段+排序方向&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="group"&gt;分组字段&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;returns&gt;结果集&lt;/returns&gt;</span></span><br><span class="line">        <span class="function">PageInfo <span class="title">Query</span><span class="params">(<span class="keyword">int</span> index, <span class="keyword">int</span> pageSize, <span class="built_in">string</span> tableName, <span class="built_in">string</span> field, <span class="built_in">string</span> filter, <span class="built_in">string</span> orderby, <span class="built_in">string</span> group, params DbParameter[] para)</span></span>;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 简单的Sql查询分页</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="index"&gt;&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="pageSize"&gt;&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="sql"&gt;&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;returns&gt;&lt;/returns&gt;</span></span><br><span class="line">        <span class="function">PageInfo <span class="title">Query</span><span class="params">(<span class="keyword">int</span> index, <span class="keyword">int</span> pageSize, <span class="built_in">string</span> sql, <span class="built_in">string</span> orderby, params DbParameter[] para)</span></span>;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 多表联合分页算法</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function">PageInfo <span class="title">Query</span><span class="params">(IQueryable query, <span class="keyword">int</span> index, <span class="keyword">int</span> pagesize)</span></span>;</span><br><span class="line">        <span class="meta">#endregion</span></span><br><span class="line"></span><br><span class="line">        <span class="meta">#region ADO.NET增删改查方法</span></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 执行增删改方法,含事务处理</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function">object <span class="title">ExecuteSqlCommand</span><span class="params">(<span class="built_in">string</span> sql, params DbParameter[] para)</span></span>;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 执行多条SQL，增删改方法,含事务处理</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function">object <span class="title">ExecuteSqlCommand</span><span class="params">(Dictionary&lt;<span class="built_in">string</span>, object&gt; sqllist)</span></span>;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 执行查询方法,返回动态类，接收使用var，遍历时使用dynamic类型</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function">object <span class="title">ExecuteSqlQuery</span><span class="params">(<span class="built_in">string</span> sql, params DbParameter[] para)</span></span>;</span><br><span class="line">        <span class="meta">#endregion</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>ReposityBase的实现：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br><span class="line">397</span><br><span class="line">398</span><br><span class="line">399</span><br><span class="line">400</span><br><span class="line">401</span><br><span class="line">402</span><br><span class="line">403</span><br><span class="line">404</span><br><span class="line">405</span><br><span class="line">406</span><br><span class="line">407</span><br><span class="line">408</span><br><span class="line">409</span><br><span class="line">410</span><br><span class="line">411</span><br><span class="line">412</span><br><span class="line">413</span><br><span class="line">414</span><br><span class="line">415</span><br><span class="line">416</span><br><span class="line">417</span><br><span class="line">418</span><br><span class="line">419</span><br><span class="line">420</span><br><span class="line">421</span><br><span class="line">422</span><br><span class="line">423</span><br><span class="line">424</span><br><span class="line">425</span><br><span class="line">426</span><br><span class="line">427</span><br><span class="line">428</span><br><span class="line">429</span><br><span class="line">430</span><br><span class="line">431</span><br><span class="line">432</span><br><span class="line">433</span><br><span class="line">434</span><br><span class="line">435</span><br><span class="line">436</span><br><span class="line">437</span><br><span class="line">438</span><br><span class="line">439</span><br><span class="line">440</span><br><span class="line">441</span><br><span class="line">442</span><br><span class="line">443</span><br><span class="line">444</span><br><span class="line">445</span><br><span class="line">446</span><br><span class="line">447</span><br><span class="line">448</span><br><span class="line">449</span><br><span class="line">450</span><br><span class="line">451</span><br><span class="line">452</span><br><span class="line">453</span><br><span class="line">454</span><br><span class="line">455</span><br><span class="line">456</span><br><span class="line">457</span><br><span class="line">458</span><br><span class="line">459</span><br><span class="line">460</span><br><span class="line">461</span><br><span class="line">462</span><br><span class="line">463</span><br><span class="line">464</span><br><span class="line">465</span><br><span class="line">466</span><br><span class="line">467</span><br><span class="line">468</span><br><span class="line">469</span><br><span class="line">470</span><br><span class="line">471</span><br><span class="line">472</span><br><span class="line">473</span><br><span class="line">474</span><br><span class="line">475</span><br><span class="line">476</span><br><span class="line">477</span><br><span class="line">478</span><br><span class="line">479</span><br><span class="line">480</span><br><span class="line">481</span><br><span class="line">482</span><br><span class="line">483</span><br><span class="line">484</span><br><span class="line">485</span><br><span class="line">486</span><br><span class="line">487</span><br><span class="line">488</span><br><span class="line">489</span><br><span class="line">490</span><br><span class="line">491</span><br><span class="line">492</span><br><span class="line">493</span><br><span class="line">494</span><br><span class="line">495</span><br><span class="line">496</span><br><span class="line">497</span><br><span class="line">498</span><br><span class="line">499</span><br><span class="line">500</span><br><span class="line">501</span><br><span class="line">502</span><br><span class="line">503</span><br><span class="line">504</span><br><span class="line">505</span><br><span class="line">506</span><br><span class="line">507</span><br><span class="line">508</span><br><span class="line">509</span><br><span class="line">510</span><br><span class="line">511</span><br><span class="line">512</span><br><span class="line">513</span><br><span class="line">514</span><br><span class="line">515</span><br><span class="line">516</span><br><span class="line">517</span><br><span class="line">518</span><br><span class="line">519</span><br><span class="line">520</span><br><span class="line">521</span><br><span class="line">522</span><br><span class="line">523</span><br><span class="line">524</span><br><span class="line">525</span><br><span class="line">526</span><br><span class="line">527</span><br><span class="line">528</span><br><span class="line">529</span><br><span class="line">530</span><br><span class="line">531</span><br><span class="line">532</span><br><span class="line">533</span><br><span class="line">534</span><br><span class="line">535</span><br><span class="line">536</span><br><span class="line">537</span><br><span class="line">538</span><br><span class="line">539</span><br><span class="line">540</span><br><span class="line">541</span><br><span class="line">542</span><br><span class="line">543</span><br><span class="line">544</span><br><span class="line">545</span><br><span class="line">546</span><br><span class="line">547</span><br><span class="line">548</span><br><span class="line">549</span><br><span class="line">550</span><br><span class="line">551</span><br><span class="line">552</span><br><span class="line">553</span><br><span class="line">554</span><br><span class="line">555</span><br><span class="line">556</span><br><span class="line">557</span><br><span class="line">558</span><br><span class="line">559</span><br><span class="line">560</span><br><span class="line">561</span><br><span class="line">562</span><br><span class="line">563</span><br><span class="line">564</span><br><span class="line">565</span><br><span class="line">566</span><br><span class="line">567</span><br><span class="line">568</span><br><span class="line">569</span><br><span class="line">570</span><br><span class="line">571</span><br><span class="line">572</span><br><span class="line">573</span><br><span class="line">574</span><br><span class="line">575</span><br><span class="line">576</span><br><span class="line">577</span><br><span class="line">578</span><br><span class="line">579</span><br><span class="line">580</span><br><span class="line">581</span><br><span class="line">582</span><br><span class="line">583</span><br><span class="line">584</span><br><span class="line">585</span><br><span class="line">586</span><br><span class="line">587</span><br><span class="line">588</span><br><span class="line">589</span><br><span class="line">590</span><br><span class="line">591</span><br><span class="line">592</span><br><span class="line">593</span><br><span class="line">594</span><br><span class="line">595</span><br><span class="line">596</span><br><span class="line">597</span><br><span class="line">598</span><br><span class="line">599</span><br><span class="line">600</span><br><span class="line">601</span><br><span class="line">602</span><br><span class="line">603</span><br><span class="line">604</span><br><span class="line">605</span><br><span class="line">606</span><br><span class="line">607</span><br><span class="line">608</span><br><span class="line">609</span><br><span class="line">610</span><br><span class="line">611</span><br><span class="line">612</span><br><span class="line">613</span><br><span class="line">614</span><br><span class="line">615</span><br><span class="line">616</span><br><span class="line">617</span><br><span class="line">618</span><br><span class="line">619</span><br><span class="line">620</span><br><span class="line">621</span><br><span class="line">622</span><br><span class="line">623</span><br><span class="line">624</span><br><span class="line">625</span><br><span class="line">626</span><br><span class="line">627</span><br><span class="line">628</span><br><span class="line">629</span><br><span class="line">630</span><br><span class="line">631</span><br><span class="line">632</span><br><span class="line">633</span><br><span class="line">634</span><br><span class="line">635</span><br><span class="line">636</span><br><span class="line">637</span><br><span class="line">638</span><br><span class="line">639</span><br><span class="line">640</span><br><span class="line">641</span><br><span class="line">642</span><br><span class="line">643</span><br><span class="line">644</span><br><span class="line">645</span><br><span class="line">646</span><br><span class="line">647</span><br><span class="line">648</span><br><span class="line">649</span><br><span class="line">650</span><br><span class="line">651</span><br><span class="line">652</span><br><span class="line">653</span><br><span class="line">654</span><br><span class="line">655</span><br><span class="line">656</span><br><span class="line">657</span><br><span class="line">658</span><br><span class="line">659</span><br><span class="line">660</span><br><span class="line">661</span><br><span class="line">662</span><br><span class="line">663</span><br><span class="line">664</span><br><span class="line">665</span><br><span class="line">666</span><br><span class="line">667</span><br><span class="line">668</span><br><span class="line">669</span><br><span class="line">670</span><br><span class="line">671</span><br><span class="line">672</span><br><span class="line">673</span><br><span class="line">674</span><br><span class="line">675</span><br><span class="line">676</span><br><span class="line">677</span><br><span class="line">678</span><br><span class="line">679</span><br><span class="line">680</span><br><span class="line">681</span><br><span class="line">682</span><br><span class="line">683</span><br><span class="line">684</span><br><span class="line">685</span><br><span class="line">686</span><br><span class="line">687</span><br><span class="line">688</span><br><span class="line">689</span><br><span class="line">690</span><br><span class="line">691</span><br><span class="line">692</span><br><span class="line">693</span><br><span class="line">694</span><br><span class="line">695</span><br><span class="line">696</span><br><span class="line">697</span><br><span class="line">698</span><br><span class="line">699</span><br><span class="line">700</span><br><span class="line">701</span><br><span class="line">702</span><br><span class="line">703</span><br><span class="line">704</span><br><span class="line">705</span><br><span class="line">706</span><br><span class="line">707</span><br><span class="line">708</span><br><span class="line">709</span><br><span class="line">710</span><br><span class="line">711</span><br><span class="line">712</span><br><span class="line">713</span><br><span class="line">714</span><br><span class="line">715</span><br><span class="line">716</span><br><span class="line">717</span><br><span class="line">718</span><br><span class="line">719</span><br><span class="line">720</span><br><span class="line">721</span><br><span class="line">722</span><br><span class="line">723</span><br><span class="line">724</span><br><span class="line">725</span><br><span class="line">726</span><br><span class="line">727</span><br><span class="line">728</span><br><span class="line">729</span><br><span class="line">730</span><br><span class="line">731</span><br><span class="line">732</span><br><span class="line">733</span><br><span class="line">734</span><br><span class="line">735</span><br><span class="line">736</span><br><span class="line">737</span><br><span class="line">738</span><br><span class="line">739</span><br><span class="line">740</span><br><span class="line">741</span><br><span class="line">742</span><br><span class="line">743</span><br><span class="line">744</span><br><span class="line">745</span><br><span class="line">746</span><br><span class="line">747</span><br><span class="line">748</span><br><span class="line">749</span><br><span class="line">750</span><br><span class="line">751</span><br><span class="line">752</span><br><span class="line">753</span><br><span class="line">754</span><br><span class="line">755</span><br><span class="line">756</span><br><span class="line">757</span><br><span class="line">758</span><br><span class="line">759</span><br><span class="line">760</span><br><span class="line">761</span><br><span class="line">762</span><br><span class="line">763</span><br><span class="line">764</span><br><span class="line">765</span><br><span class="line">766</span><br><span class="line">767</span><br><span class="line">768</span><br><span class="line">769</span><br><span class="line">770</span><br><span class="line">771</span><br><span class="line">772</span><br><span class="line">773</span><br><span class="line">774</span><br><span class="line">775</span><br><span class="line">776</span><br><span class="line">777</span><br><span class="line">778</span><br><span class="line">779</span><br><span class="line">780</span><br><span class="line">781</span><br><span class="line">782</span><br><span class="line">783</span><br><span class="line">784</span><br><span class="line">785</span><br><span class="line">786</span><br><span class="line">787</span><br><span class="line">788</span><br><span class="line">789</span><br><span class="line">790</span><br><span class="line">791</span><br><span class="line">792</span><br><span class="line">793</span><br><span class="line">794</span><br><span class="line">795</span><br><span class="line">796</span><br><span class="line">797</span><br><span class="line">798</span><br><span class="line">799</span><br><span class="line">800</span><br><span class="line">801</span><br><span class="line">802</span><br><span class="line">803</span><br><span class="line">804</span><br><span class="line">805</span><br><span class="line">806</span><br><span class="line">807</span><br><span class="line">808</span><br><span class="line">809</span><br><span class="line">810</span><br><span class="line">811</span><br><span class="line">812</span><br><span class="line">813</span><br><span class="line">814</span><br><span class="line">815</span><br><span class="line">816</span><br><span class="line">817</span><br><span class="line">818</span><br><span class="line">819</span><br><span class="line">820</span><br><span class="line">821</span><br><span class="line">822</span><br><span class="line">823</span><br><span class="line">824</span><br><span class="line">825</span><br><span class="line">826</span><br><span class="line">827</span><br><span class="line">828</span><br><span class="line">829</span><br><span class="line">830</span><br><span class="line">831</span><br><span class="line">832</span><br><span class="line">833</span><br><span class="line">834</span><br><span class="line">835</span><br><span class="line">836</span><br><span class="line">837</span><br><span class="line">838</span><br><span class="line">839</span><br><span class="line">840</span><br><span class="line">841</span><br><span class="line">842</span><br><span class="line">843</span><br><span class="line">844</span><br><span class="line">845</span><br><span class="line">846</span><br><span class="line">847</span><br><span class="line">848</span><br><span class="line">849</span><br><span class="line">850</span><br><span class="line">851</span><br><span class="line">852</span><br><span class="line">853</span><br><span class="line">854</span><br><span class="line">855</span><br><span class="line">856</span><br><span class="line">857</span><br><span class="line">858</span><br><span class="line">859</span><br><span class="line">860</span><br><span class="line">861</span><br><span class="line">862</span><br><span class="line">863</span><br><span class="line">864</span><br><span class="line">865</span><br><span class="line">866</span><br><span class="line">867</span><br><span class="line">868</span><br><span class="line">869</span><br><span class="line">870</span><br><span class="line">871</span><br><span class="line">872</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;</span><br><span class="line"><span class="keyword">using</span> System.Collections.Generic;</span><br><span class="line"><span class="keyword">using</span> System.Linq;</span><br><span class="line"><span class="keyword">using</span> System.Data;</span><br><span class="line"><span class="keyword">using</span> System.Data.Common;</span><br><span class="line"><span class="keyword">using</span> System.Data.Entity;</span><br><span class="line"><span class="keyword">using</span> System.Data.Entity.Infrastructure;</span><br><span class="line"><span class="keyword">using</span> Domain;</span><br><span class="line"><span class="keyword">using</span> System.Linq.Expressions;</span><br><span class="line"><span class="keyword">using</span> System.Collections;</span><br><span class="line"><span class="keyword">using</span> Common;</span><br><span class="line"><span class="keyword">using</span> Common.JsonHelper;</span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> Service</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">    <span class="comment">/// 数据操作基本实现类，公用实现方法</span></span><br><span class="line">    <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">    <span class="comment">/// &lt;typeparam name="T"&gt;具体操作的实体模型&lt;/typeparam&gt;</span></span><br><span class="line">    <span class="keyword">public</span> abstract <span class="class"><span class="keyword">class</span> <span class="title">RepositoryBase</span>&lt;T&gt; :</span> IRepository&lt;T&gt; where T : <span class="class"><span class="keyword">class</span></span></span><br><span class="line"><span class="class">    &#123;</span></span><br><span class="line">        <span class="meta">#region 固定公用帮助，含事务</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">private</span> DbContext context = <span class="keyword">new</span> MyConfig().db;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 数据上下文---&gt;根据Domain实体模型名称进行更改</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">public</span> DbContext Context</span><br><span class="line">        &#123;</span><br><span class="line">            get</span><br><span class="line">            &#123;</span><br><span class="line">                context.Configuration.ValidateOnSaveEnabled = <span class="literal">false</span>;</span><br><span class="line">                <span class="keyword">return</span> context;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 数据上下文---&gt;拓展属性</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">public</span> MyConfig Config</span><br><span class="line">        &#123;</span><br><span class="line">            get</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">new</span> MyConfig();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 公用泛型处理属性</span></span><br><span class="line">        <span class="comment">/// 注:所有泛型操作的基础</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">public</span> DbSet&lt;T&gt; dbSet</span><br><span class="line">        &#123;</span><br><span class="line">            get &#123; <span class="keyword">return</span> <span class="keyword">this</span>.Context.Set&lt;T&gt;(); &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 事务</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">private</span> DbContextTransaction _transaction = null;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 开始事务</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">public</span> DbContextTransaction Transaction</span><br><span class="line">        &#123;</span><br><span class="line">            get</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">if</span> (<span class="keyword">this</span>._transaction == null)</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">this</span>._transaction = <span class="keyword">this</span>.Context.Database.BeginTransaction();</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>._transaction;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="built_in">set</span> &#123; <span class="keyword">this</span>._transaction = value; &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 事务状态</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">bool</span> Committed &#123; get; <span class="built_in">set</span>; &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 异步锁定</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">private</span> readonly object sync = <span class="keyword">new</span> object();</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 提交事务</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Commit</span><span class="params">()</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (!Committed)</span><br><span class="line">            &#123;</span><br><span class="line">                lock (sync)</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">if</span> (<span class="keyword">this</span>._transaction != null)</span><br><span class="line">                        _transaction.Commit();</span><br><span class="line">                &#125;</span><br><span class="line">                Committed = <span class="literal">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 回滚事务</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Rollback</span><span class="params">()</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            Committed = <span class="literal">false</span>;</span><br><span class="line">            <span class="keyword">if</span> (<span class="keyword">this</span>._transaction != null)</span><br><span class="line">                <span class="keyword">this</span>._transaction.Rollback();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="meta">#endregion</span></span><br><span class="line"></span><br><span class="line">        <span class="meta">#region 获取单条记录</span></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 通过lambda表达式获取一条记录p=&gt;p.id==id</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> T <span class="title">Get</span><span class="params">(Expression&lt;Func&lt;T, <span class="keyword">bool</span>&gt;&gt; predicate)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">return</span> dbSet.AsNoTracking().SingleOrDefault(predicate);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="meta">#endregion</span></span><br><span class="line"></span><br><span class="line">        <span class="meta">#region 增删改操作</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 添加一条模型记录，自动提交更改</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="title">Save</span><span class="params">(T entity)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">int</span> row = <span class="number">0</span>;</span><br><span class="line">                var entry = <span class="keyword">this</span>.Context.Entry&lt;T&gt;(entity);</span><br><span class="line">                entry.State = System.Data.Entity.EntityState.Added;</span><br><span class="line">                row = Context.SaveChanges();</span><br><span class="line">                entry.State = System.Data.Entity.EntityState.Detached;</span><br><span class="line">                <span class="keyword">return</span> row &gt; <span class="number">0</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 更新一条模型记录，自动提交更改</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="title">Update</span><span class="params">(T entity)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">int</span> rows = <span class="number">0</span>;</span><br><span class="line">                var entry = <span class="keyword">this</span>.Context.Entry(entity);</span><br><span class="line">                entry.State = System.Data.Entity.EntityState.Modified;</span><br><span class="line">                rows = <span class="keyword">this</span>.Context.SaveChanges();</span><br><span class="line">                entry.State = System.Data.Entity.EntityState.Detached;</span><br><span class="line">                <span class="keyword">return</span> rows &gt; <span class="number">0</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 更新模型记录，如不存在进行添加操作</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="title">SaveOrUpdate</span><span class="params">(T entity, <span class="keyword">bool</span> isEdit)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">return</span> isEdit ? Update(entity) : Save(entity);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e) &#123; <span class="keyword">throw</span> e; &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 删除一条或多条模型记录，含事务</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> <span class="keyword">int</span> <span class="title">Delete</span><span class="params">(Expression&lt;Func&lt;T, <span class="keyword">bool</span>&gt;&gt; predicate = null)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">int</span> rows = <span class="number">0</span>;</span><br><span class="line">                IQueryable&lt;T&gt; entry = (predicate == null) ? <span class="keyword">this</span>.dbSet.AsQueryable() : <span class="keyword">this</span>.dbSet.Where(predicate);</span><br><span class="line">                List&lt;T&gt; <span class="built_in">list</span> = entry.ToList();</span><br><span class="line">                <span class="keyword">if</span> (<span class="built_in">list</span>.Count &gt; <span class="number">0</span>)</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="built_in">list</span>.Count; i++)</span><br><span class="line">                    &#123;</span><br><span class="line">                        <span class="keyword">this</span>.dbSet.Remove(<span class="built_in">list</span>[i]);</span><br><span class="line">                    &#125;</span><br><span class="line">                    rows = <span class="keyword">this</span>.Context.SaveChanges();</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span> rows;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 使用原始SQL语句,含事务处理</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> <span class="keyword">int</span> <span class="title">DeleteBySql</span><span class="params">(<span class="built_in">string</span> sql, params DbParameter[] para)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.Context.Database.ExecuteSqlCommand(sql, para);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="meta">#endregion</span></span><br><span class="line"></span><br><span class="line">        <span class="meta">#region 多模型操作</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 增加多模型数据，指定独立模型集合</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">virtual</span> <span class="keyword">int</span> SaveList&lt;T1&gt;(List&lt;T1&gt; t) where T1 : <span class="class"><span class="keyword">class</span></span></span><br><span class="line"><span class="class">        &#123;</span></span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">if</span> (t == null || t.Count == <span class="number">0</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">                <span class="keyword">this</span>.Context.Set&lt;T1&gt;().Local.Clear();</span><br><span class="line">                foreach (var item in t)</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">this</span>.Context.Set&lt;T1&gt;().Add(item);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.Context.SaveChanges();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 增加多模型数据，与当前模型一致</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> <span class="keyword">int</span> <span class="title">SaveList</span><span class="params">(List&lt;T&gt; t)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">this</span>.dbSet.Local.Clear();</span><br><span class="line">                foreach (var item in t)</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">this</span>.dbSet.Add(item);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.Context.SaveChanges();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 更新多模型，指定独立模型集合</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">virtual</span> <span class="keyword">int</span> UpdateList&lt;T1&gt;(List&lt;T1&gt; t) where T1 : <span class="class"><span class="keyword">class</span></span></span><br><span class="line"><span class="class">        &#123;</span></span><br><span class="line">            <span class="keyword">if</span> (t.Count &lt;= <span class="number">0</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                foreach (var item in t)</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">this</span>.Context.Entry&lt;T1&gt;(item).State = System.Data.Entity.EntityState.Modified;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.Context.SaveChanges();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 更新多模型，与当前模型一致</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> <span class="keyword">int</span> <span class="title">UpdateList</span><span class="params">(List&lt;T&gt; t)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (t.Count &lt;= <span class="number">0</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                foreach (var item in t)</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">this</span>.Context.Entry(item).State = System.Data.Entity.EntityState.Modified;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.Context.SaveChanges();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e) &#123; <span class="keyword">throw</span> e; &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 批量删除数据，当前模型</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> <span class="keyword">int</span> <span class="title">DeleteList</span><span class="params">(List&lt;T&gt; t)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (t == null || t.Count == <span class="number">0</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">            foreach (var item in t)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">this</span>.dbSet.Remove(item);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">this</span>.Context.SaveChanges();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 批量删除数据，自定义模型</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">virtual</span> <span class="keyword">int</span> DeleteList&lt;T1&gt;(List&lt;T1&gt; t) where T1 : <span class="class"><span class="keyword">class</span></span></span><br><span class="line"><span class="class">        &#123;</span></span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">if</span> (t == null || t.Count == <span class="number">0</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">                foreach (var item in t)</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">this</span>.Context.Set&lt;T1&gt;().Remove(item);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.Context.SaveChanges();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e) &#123; <span class="keyword">throw</span> e; &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="meta">#endregion</span></span><br><span class="line"></span><br><span class="line">        <span class="meta">#region 存储过程操作</span></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 执行返回影响行数的存储过程</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="procname"&gt;过程名称&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="parameter"&gt;参数对象&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;returns&gt;&lt;/returns&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> object <span class="title">ExecuteProc</span><span class="params">(<span class="built_in">string</span> procname, params DbParameter[] parameter)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">return</span> ExecuteSqlCommand(procname, parameter);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 执行返回结果集的存储过程</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="procname"&gt;过程名称&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="parameter"&gt;参数对象&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;returns&gt;&lt;/returns&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> object <span class="title">ExecuteQueryProc</span><span class="params">(<span class="built_in">string</span> procname, params DbParameter[] parameter)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.Context.Database.SqlFunctionForDynamic(procname, parameter);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="meta">#endregion</span></span><br><span class="line"></span><br><span class="line">        <span class="meta">#region 存在验证操作</span></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 验证当前条件是否存在相同项</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="title">IsExist</span><span class="params">(Expression&lt;Func&lt;T, <span class="keyword">bool</span>&gt;&gt; predicate)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            var entry = <span class="keyword">this</span>.dbSet.Where(predicate);</span><br><span class="line">            <span class="keyword">return</span> (entry.Any());</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 根据SQL验证实体对象是否存在</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="title">IsExist</span><span class="params">(<span class="built_in">string</span> sql, params DbParameter[] para)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            IEnumerable result = <span class="keyword">this</span>.Context.Database.SqlQuery(typeof(<span class="keyword">int</span>), sql, para);</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (result.GetEnumerator().Current == null || result.GetEnumerator().Current.ToString() == <span class="string">"0"</span>)</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="meta">#endregion</span></span><br><span class="line"></span><br><span class="line">        <span class="meta">#region 获取多条数据操作</span></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 返回IQueryable集合，延时加载数据</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">virtual</span> IQueryable&lt;T&gt; LoadAll(Expression&lt;Func&lt;T, <span class="keyword">bool</span>&gt;&gt; predicate)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">if</span> (predicate != null)</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">return</span> <span class="keyword">this</span>.dbSet.Where(predicate).AsNoTracking&lt;T&gt;();</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.dbSet.AsQueryable&lt;T&gt;().AsNoTracking&lt;T&gt;();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 返回DbQuery集合，延时加载数据</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">virtual</span> DbQuery&lt;T&gt; LoadQueryAll(Expression&lt;Func&lt;T, <span class="keyword">bool</span>&gt;&gt; predicate)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">if</span> (predicate != null)</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">return</span> <span class="keyword">this</span>.dbSet.Where(predicate) as DbQuery&lt;T&gt;;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.dbSet;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 返回List集合,不采用延时加载</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">virtual</span> List&lt;T&gt; LoadListAll(Expression&lt;Func&lt;T, <span class="keyword">bool</span>&gt;&gt; predicate)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">if</span> (predicate != null)</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">return</span> <span class="keyword">this</span>.dbSet.Where(predicate).AsNoTracking().ToList();</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.dbSet.AsQueryable&lt;T&gt;().AsNoTracking().ToList();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 返回IEnumerable集合，采用原始T-Sql方式</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">virtual</span> IEnumerable&lt;T&gt; LoadEnumerableAll(<span class="built_in">string</span> sql, params DbParameter[] para)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.Context.Database.SqlQuery&lt;T&gt;(sql, para);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 返回IEnumerable动态集合，采用原始T-Sql方式</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> IEnumerable <span class="title">LoadEnumerable</span><span class="params">(<span class="built_in">string</span> sql, params DbParameter[] para)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.Context.Database.SqlQueryForDynamic(sql, para);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 返回IList集合，采用原始T-Sql方式</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">virtual</span> List&lt;T&gt; SelectBySql(<span class="built_in">string</span> sql, params DbParameter[] para)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.Context.Database.SqlQuery(typeof(T), sql, para).Cast&lt;T&gt;().ToList();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 指定泛型，返回IList集合，采用原始T-Sql方式</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">virtual</span> List&lt;T1&gt; SelectBySql&lt;T1&gt;(<span class="built_in">string</span> sql, params DbParameter[] para)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.Context.Database.SqlQuery&lt;T1&gt;(sql, para).ToList();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 可指定返回结果、排序、查询条件的通用查询方法，返回实体对象</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;typeparam name="TEntity"&gt;实体对象&lt;/typeparam&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;typeparam name="TOrderBy"&gt;排序字段类型&lt;/typeparam&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;typeparam name="TResult"&gt;数据结果，与TEntity一致&lt;/typeparam&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="where"&gt;过滤条件，需要用到类型转换的需要提前处理与数据表一致的&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="orderby"&gt;排序字段&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="selector"&gt;返回结果（必须是模型中存在的字段）&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="IsAsc"&gt;排序方向，true为正序false为倒序&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;returns&gt;实体集合&lt;/returns&gt;</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">virtual</span> List&lt;TResult&gt; QueryEntity&lt;TEntity, TOrderBy, TResult&gt;</span><br><span class="line">            (Expression&lt;Func&lt;TEntity, <span class="keyword">bool</span>&gt;&gt; where,</span><br><span class="line">            Expression&lt;Func&lt;TEntity, TOrderBy&gt;&gt; orderby,</span><br><span class="line">            Expression&lt;Func&lt;TEntity, TResult&gt;&gt; selector,</span><br><span class="line">            <span class="keyword">bool</span> IsAsc)</span><br><span class="line">            where TEntity : <span class="class"><span class="keyword">class</span></span></span><br><span class="line"><span class="class">            <span class="title">where</span> <span class="title">TResult</span> :</span> <span class="class"><span class="keyword">class</span></span></span><br><span class="line"><span class="class">        &#123;</span></span><br><span class="line">            IQueryable&lt;TEntity&gt; query = <span class="keyword">this</span>.Context.Set&lt;TEntity&gt;();</span><br><span class="line">            <span class="keyword">if</span> (where != null)</span><br><span class="line">            &#123;</span><br><span class="line">                query = query.Where(where);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (orderby != null)</span><br><span class="line">            &#123;</span><br><span class="line">                query = IsAsc ? query.OrderBy(orderby) : query.OrderByDescending(orderby);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (selector == null)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">return</span> query.Cast&lt;TResult&gt;().AsNoTracking().ToList();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> query.Select(selector).AsNoTracking().ToList();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 可指定返回结果、排序、查询条件的通用查询方法，返回Object对象</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;typeparam name="TEntity"&gt;实体对象&lt;/typeparam&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;typeparam name="TOrderBy"&gt;排序字段类型&lt;/typeparam&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="where"&gt;过滤条件，需要用到类型转换的需要提前处理与数据表一致的&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="orderby"&gt;排序字段&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="selector"&gt;返回结果（必须是模型中存在的字段）&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="IsAsc"&gt;排序方向，true为正序false为倒序&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;returns&gt;自定义实体集合&lt;/returns&gt;</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">virtual</span> List&lt;object&gt; QueryObject&lt;TEntity, TOrderBy&gt;</span><br><span class="line">            (Expression&lt;Func&lt;TEntity, <span class="keyword">bool</span>&gt;&gt; where,</span><br><span class="line">            Expression&lt;Func&lt;TEntity, TOrderBy&gt;&gt; orderby,</span><br><span class="line">            Func&lt;IQueryable&lt;TEntity&gt;,</span><br><span class="line">            List&lt;object&gt;&gt; selector,</span><br><span class="line">            <span class="keyword">bool</span> IsAsc)</span><br><span class="line">            where TEntity : <span class="class"><span class="keyword">class</span></span></span><br><span class="line"><span class="class">        &#123;</span></span><br><span class="line">            IQueryable&lt;TEntity&gt; query = <span class="keyword">this</span>.Context.Set&lt;TEntity&gt;();</span><br><span class="line">            <span class="keyword">if</span> (where != null)</span><br><span class="line">            &#123;</span><br><span class="line">                query = query.Where(where);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (orderby != null)</span><br><span class="line">            &#123;</span><br><span class="line">                query = IsAsc ? query.OrderBy(orderby) : query.OrderByDescending(orderby);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (selector == null)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">return</span> query.AsNoTracking().ToList&lt;object&gt;();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> selector(query);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 可指定返回结果、排序、查询条件的通用查询方法，返回动态类对象</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;typeparam name="TEntity"&gt;实体对象&lt;/typeparam&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;typeparam name="TOrderBy"&gt;排序字段类型&lt;/typeparam&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="where"&gt;过滤条件，需要用到类型转换的需要提前处理与数据表一致的&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="orderby"&gt;排序字段&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="selector"&gt;返回结果（必须是模型中存在的字段）&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="IsAsc"&gt;排序方向，true为正序false为倒序&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;returns&gt;动态类&lt;/returns&gt;</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">virtual</span> dynamic QueryDynamic&lt;TEntity, TOrderBy&gt;</span><br><span class="line">            (Expression&lt;Func&lt;TEntity, <span class="keyword">bool</span>&gt;&gt; where,</span><br><span class="line">            Expression&lt;Func&lt;TEntity, TOrderBy&gt;&gt; orderby,</span><br><span class="line">            Func&lt;IQueryable&lt;TEntity&gt;,</span><br><span class="line">            List&lt;object&gt;&gt; selector,</span><br><span class="line">            <span class="keyword">bool</span> IsAsc)</span><br><span class="line">            where TEntity : <span class="class"><span class="keyword">class</span></span></span><br><span class="line"><span class="class">        &#123;</span></span><br><span class="line">            List&lt;object&gt; <span class="built_in">list</span> = QueryObject&lt;TEntity, TOrderBy&gt;</span><br><span class="line">                 (where, orderby, selector, IsAsc);</span><br><span class="line">            <span class="keyword">return</span> JsonConverter.JsonClass(<span class="built_in">list</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="meta">#endregion</span></span><br><span class="line"></span><br><span class="line">        <span class="meta">#region 分页操作</span></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 待自定义分页函数，使用必须重写，指定数据模型</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">virtual</span> IList&lt;T1&gt; PageByListSql&lt;T1&gt;(<span class="built_in">string</span> sql, IList&lt;DbParameter&gt; parameters, PageCollection page)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">return</span> null;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 待自定义分页函数，使用必须重写，</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">virtual</span> IList&lt;T&gt; PageByListSql(<span class="built_in">string</span> sql, IList&lt;DbParameter&gt; parameters, PageCollection page)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">return</span> null;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 对IQueryable对象进行分页逻辑处理，过滤、查询项、排序对IQueryable操作</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="t"&gt;Iqueryable&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="index"&gt;当前页&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="pageSize"&gt;每页显示多少条&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;returns&gt;当前IQueryable to List的对象&lt;/returns&gt;</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">virtual</span> PageInfo&lt;T&gt; Query(IQueryable&lt;T&gt; query, <span class="keyword">int</span> index, <span class="keyword">int</span> pageSize)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">if</span> (index &lt; <span class="number">1</span>)</span><br><span class="line">            &#123;</span><br><span class="line">                index = <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (pageSize &lt;= <span class="number">0</span>)</span><br><span class="line">            &#123;</span><br><span class="line">                pageSize = <span class="number">20</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">int</span> count = query.Count();</span><br><span class="line"></span><br><span class="line">            <span class="keyword">int</span> maxpage = count / pageSize;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (count % pageSize &gt; <span class="number">0</span>)</span><br><span class="line">            &#123;</span><br><span class="line">                maxpage++;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (index &gt; maxpage)</span><br><span class="line">            &#123;</span><br><span class="line">                index = maxpage;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (count &gt; <span class="number">0</span>)</span><br><span class="line">                query = query.Skip((index - <span class="number">1</span>) * pageSize).Take(pageSize);</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> PageInfo&lt;T&gt;(index, pageSize, count, query.ToList());</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 通用EF分页，默认显示20条记录</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;typeparam name="TEntity"&gt;实体模型&lt;/typeparam&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;typeparam name="TOrderBy"&gt;排序类型&lt;/typeparam&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="index"&gt;当前页&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="pageSize"&gt;显示条数&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="where"&gt;过滤条件&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="orderby"&gt;排序字段&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="selector"&gt;结果集合&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="isAsc"&gt;排序方向true正序 false倒序&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;returns&gt;自定义实体集合&lt;/returns&gt;</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">virtual</span> PageInfo&lt;object&gt; Query&lt;TEntity, TOrderBy&gt;</span><br><span class="line">            (<span class="keyword">int</span> index, <span class="keyword">int</span> pageSize,</span><br><span class="line">            Expression&lt;Func&lt;TEntity, <span class="keyword">bool</span>&gt;&gt; where,</span><br><span class="line">            Expression&lt;Func&lt;TEntity, TOrderBy&gt;&gt; orderby,</span><br><span class="line">            Func&lt;IQueryable&lt;TEntity&gt;,</span><br><span class="line">            List&lt;object&gt;&gt; selector,</span><br><span class="line">            <span class="keyword">bool</span> isAsc)</span><br><span class="line">            where TEntity : <span class="class"><span class="keyword">class</span></span></span><br><span class="line"><span class="class">        &#123;</span></span><br><span class="line">            <span class="keyword">if</span> (index &lt; <span class="number">1</span>)</span><br><span class="line">            &#123;</span><br><span class="line">                index = <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (pageSize &lt;= <span class="number">0</span>)</span><br><span class="line">            &#123;</span><br><span class="line">                pageSize = <span class="number">20</span>;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            IQueryable&lt;TEntity&gt; query = <span class="keyword">this</span>.Context.Set&lt;TEntity&gt;();</span><br><span class="line">            <span class="keyword">if</span> (where != null)</span><br><span class="line">            &#123;</span><br><span class="line">                query = query.Where(where);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">int</span> count = query.Count();</span><br><span class="line"></span><br><span class="line">            <span class="keyword">int</span> maxpage = count / pageSize;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (count % pageSize &gt; <span class="number">0</span>)</span><br><span class="line">            &#123;</span><br><span class="line">                maxpage++;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (index &gt; maxpage)</span><br><span class="line">            &#123;</span><br><span class="line">                index = maxpage;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (orderby != null)</span><br><span class="line">            &#123;</span><br><span class="line">                query = isAsc ? query.OrderBy(orderby) : query.OrderByDescending(orderby);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (count &gt; <span class="number">0</span>)</span><br><span class="line">                query = query.Skip((index - <span class="number">1</span>) * pageSize).Take(pageSize);</span><br><span class="line">            <span class="comment">//返回结果为null，返回所有字段</span></span><br><span class="line">            <span class="keyword">if</span> (selector == null)</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">new</span> PageInfo&lt;object&gt;(index, pageSize, count, query.ToList&lt;object&gt;());</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> PageInfo&lt;object&gt;(index, pageSize, count, selector(query).ToList());</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 普通SQL查询分页方法</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="index"&gt;当前页&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="pageSize"&gt;显示行数&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="tableName"&gt;表名/视图&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="field"&gt;获取项&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="filter"&gt;过滤条件&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="orderby"&gt;排序字段+排序方向&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="group"&gt;分组字段&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;returns&gt;结果集&lt;/returns&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> PageInfo <span class="title">Query</span><span class="params">(<span class="keyword">int</span> index, <span class="keyword">int</span> pageSize, <span class="built_in">string</span> tableName, <span class="built_in">string</span> field, <span class="built_in">string</span> filter, <span class="built_in">string</span> orderby, <span class="built_in">string</span> group, params DbParameter[] para)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="comment">//执行分页算法</span></span><br><span class="line">            <span class="keyword">if</span> (index &lt;= <span class="number">0</span>)</span><br><span class="line">                index = <span class="number">1</span>;</span><br><span class="line">            <span class="keyword">int</span> start = (index - <span class="number">1</span>) * pageSize;</span><br><span class="line">            <span class="keyword">if</span> (start &gt; <span class="number">0</span>)</span><br><span class="line">                start -= <span class="number">1</span>;</span><br><span class="line">            <span class="keyword">else</span></span><br><span class="line">                start = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">int</span> end = index * pageSize;</span><br><span class="line"></span><br><span class="line">            <span class="meta">#region 查询逻辑</span></span><br><span class="line">            <span class="built_in">string</span> logicSql = <span class="string">"SELECT"</span>;</span><br><span class="line">            <span class="comment">//查询项</span></span><br><span class="line">            <span class="keyword">if</span> (!<span class="built_in">string</span>.IsNullOrEmpty(field))</span><br><span class="line">            &#123;</span><br><span class="line">                logicSql += <span class="string">" "</span> + field;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span></span><br><span class="line">            &#123;</span><br><span class="line">                logicSql += <span class="string">" *"</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            logicSql += <span class="string">" FROM ("</span> + tableName + <span class="string">" ) where"</span>;</span><br><span class="line">            <span class="comment">//过滤条件</span></span><br><span class="line">            <span class="keyword">if</span> (!<span class="built_in">string</span>.IsNullOrEmpty(filter))</span><br><span class="line">            &#123;</span><br><span class="line">                logicSql += <span class="string">" "</span> + filter;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span></span><br><span class="line">            &#123;</span><br><span class="line">                filter = <span class="string">" 1=1"</span>;</span><br><span class="line">                logicSql += <span class="string">"  1=1"</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//分组</span></span><br><span class="line">            <span class="keyword">if</span> (!<span class="built_in">string</span>.IsNullOrEmpty(group))</span><br><span class="line">            &#123;</span><br><span class="line">                logicSql += <span class="string">" group by "</span> + group;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="meta">#endregion</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">//获取当前条件下数据总条数</span></span><br><span class="line">            <span class="keyword">int</span> count = <span class="keyword">this</span>.Context.Database.SqlQuery(typeof(<span class="keyword">int</span>), <span class="string">"select count(*) from ("</span> + tableName + <span class="string">") where "</span> + filter, para).Cast&lt;<span class="keyword">int</span>&gt;().FirstOrDefault();</span><br><span class="line">            <span class="built_in">string</span> sql = <span class="string">"SELECT T.* FROM ( SELECT B.* FROM ( SELECT A.*,ROW_NUMBER() OVER(ORDER BY getdate()) as RN"</span> +</span><br><span class="line">                         logicSql + <span class="string">") A ) B WHERE B.RN&lt;="</span> + end + <span class="string">") T WHERE T.RN&gt;"</span> + start;</span><br><span class="line">            <span class="comment">//排序</span></span><br><span class="line">            <span class="keyword">if</span> (!<span class="built_in">string</span>.IsNullOrEmpty(orderby))</span><br><span class="line">            &#123;</span><br><span class="line">                sql += <span class="string">" order by "</span> + orderby;</span><br><span class="line">            &#125;</span><br><span class="line">            var <span class="built_in">list</span> = ExecuteSqlQuery(sql, para) as IEnumerable;</span><br><span class="line">            <span class="keyword">if</span> (<span class="built_in">list</span> != null)</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">new</span> PageInfo(index, pageSize, count, <span class="built_in">list</span>.Cast&lt;object&gt;().ToList());</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> PageInfo(index, pageSize, count, <span class="keyword">new</span> &#123; &#125;);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 最简单的SQL分页</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="index"&gt;页码&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="pageSize"&gt;显示行数&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="sql"&gt;纯SQL语句&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;param name="orderby"&gt;排序字段与方向&lt;/param&gt;</span></span><br><span class="line">        <span class="comment">/// &lt;returns&gt;&lt;/returns&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> PageInfo <span class="title">Query</span><span class="params">(<span class="keyword">int</span> index, <span class="keyword">int</span> pageSize, <span class="built_in">string</span> sql, <span class="built_in">string</span> orderby, params DbParameter[] para)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">this</span>.Query(index, pageSize, sql, null, null, orderby, null, para);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 多表联合分页算法</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> PageInfo <span class="title">Query</span><span class="params">(IQueryable query, <span class="keyword">int</span> index, <span class="keyword">int</span> PageSize)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            var enumerable = (query as System.Collections.IEnumerable).Cast&lt;object&gt;();</span><br><span class="line">            <span class="keyword">if</span> (index &lt; <span class="number">1</span>)</span><br><span class="line">            &#123;</span><br><span class="line">                index = <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (PageSize &lt;= <span class="number">0</span>)</span><br><span class="line">            &#123;</span><br><span class="line">                PageSize = <span class="number">20</span>;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">int</span> count = enumerable.Count();</span><br><span class="line"></span><br><span class="line">            <span class="keyword">int</span> maxpage = count / PageSize;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (count % PageSize &gt; <span class="number">0</span>)</span><br><span class="line">            &#123;</span><br><span class="line">                maxpage++;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (index &gt; maxpage)</span><br><span class="line">            &#123;</span><br><span class="line">                index = maxpage;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (count &gt; <span class="number">0</span>)</span><br><span class="line">                enumerable = enumerable.Skip((index - <span class="number">1</span>) * PageSize).Take(PageSize);</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> PageInfo(index, PageSize, count, enumerable.ToList());</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="meta">#endregion</span></span><br><span class="line"></span><br><span class="line">        <span class="meta">#region ADO.NET增删改查方法</span></span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 执行增删改方法,含事务处理</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> object <span class="title">ExecuteSqlCommand</span><span class="params">(<span class="built_in">string</span> sql, params DbParameter[] para)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.Context.Database.ExecuteSqlCommand(sql, para);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 执行多条SQL，增删改方法,含事务处理</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> object <span class="title">ExecuteSqlCommand</span><span class="params">(Dictionary&lt;<span class="built_in">string</span>, object&gt; sqllist)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">int</span> rows = <span class="number">0</span>;</span><br><span class="line">                IEnumerator&lt;KeyValuePair&lt;<span class="built_in">string</span>, object&gt;&gt; enumerator = sqllist.GetEnumerator();</span><br><span class="line">                <span class="keyword">using</span> (Transaction)</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">while</span> (enumerator.MoveNext())</span><br><span class="line">                    &#123;</span><br><span class="line">                        rows += <span class="keyword">this</span>.Context.Database.ExecuteSqlCommand(enumerator.Current.Key, enumerator.Current.Value);</span><br><span class="line">                    &#125;</span><br><span class="line">                    Commit();</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span> rows;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                Rollback();</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/// &lt;summary&gt;</span></span><br><span class="line">        <span class="comment">/// 执行查询方法,返回动态类，接收使用var，遍历时使用dynamic类型</span></span><br><span class="line">        <span class="comment">/// &lt;/summary&gt;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> object <span class="title">ExecuteSqlQuery</span><span class="params">(<span class="built_in">string</span> sql, params DbParameter[] para)</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">try</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.Context.Database.SqlQueryForDynamic(sql, para);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">throw</span> e;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="meta">#endregion</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>新建一个查询动态类：DatabaseExtensions.cs</p>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;</span><br><span class="line"><span class="keyword">using</span> System.Collections;</span><br><span class="line"><span class="keyword">using</span> System.Data;</span><br><span class="line"><span class="keyword">using</span> System.Data.Entity;</span><br><span class="line"><span class="keyword">using</span> System.Reflection;</span><br><span class="line"><span class="keyword">using</span> System.Reflection.Emit;</span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">Service</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 查询动态类</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">class</span> <span class="title">DatabaseExtensions</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">        <span class="comment"><span class="doctag">///</span> 自定义Connection对象</span></span><br><span class="line">        <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">        <span class="keyword">private</span> <span class="keyword">static</span> IDbConnection DefaultConnection</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">get</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">return</span> Domain.MyConfig.DefaultConnection;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">        <span class="comment"><span class="doctag">///</span> 自定义数据库连接字符串，与EF连接模式一致</span></span><br><span class="line">        <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">        <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">string</span> DefaultConnectionString</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">get</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">return</span> Domain.MyConfig.DefaultConnectionString;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">        <span class="comment"><span class="doctag">///</span> 动态查询主方法</span></span><br><span class="line">        <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">        <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;returns&gt;</span><span class="doctag">&lt;/returns&gt;</span></span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> IEnumerable <span class="title">SqlQueryForDynamic</span>(<span class="params"><span class="keyword">this</span> Database db, <span class="keyword">string</span> sql, <span class="keyword">params</span> <span class="keyword">object</span>[] parameters</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            IDbConnection defaultConn = DefaultConnection;</span><br><span class="line"></span><br><span class="line">            <span class="comment">//ADO.NET数据库连接字符串</span></span><br><span class="line">            db.Connection.ConnectionString = DefaultConnectionString;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">return</span> SqlQueryForDynamicOtherDB(db, sql, defaultConn, parameters);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> IEnumerable <span class="title">SqlQueryForDynamicOtherDB</span>(<span class="params"><span class="keyword">this</span> Database db, <span class="keyword">string</span> sql, IDbConnection conn, <span class="keyword">params</span> <span class="keyword">object</span>[] parameters</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            conn.ConnectionString = db.Connection.ConnectionString;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (conn.State != ConnectionState.Open)</span><br><span class="line">            &#123;</span><br><span class="line">                conn.Open();</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            IDbCommand cmd = conn.CreateCommand();</span><br><span class="line">            cmd.CommandText = sql;</span><br><span class="line">            <span class="keyword">if</span> (parameters != <span class="literal">null</span>)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">foreach</span> (<span class="keyword">var</span> item <span class="keyword">in</span> parameters)</span><br><span class="line">                &#123;</span><br><span class="line">                    cmd.Parameters.Add(item);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">using</span> (IDataReader dataReader = cmd.ExecuteReader())</span><br><span class="line">            &#123;</span><br><span class="line"></span><br><span class="line">                <span class="keyword">if</span> (!dataReader.Read())</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">return</span> <span class="literal">null</span>; <span class="comment">//无结果返回Null</span></span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">                <span class="meta">#<span class="meta-keyword">region</span> 构建动态字段</span></span><br><span class="line"></span><br><span class="line">                TypeBuilder builder = DatabaseExtensions.CreateTypeBuilder(</span><br><span class="line">                    <span class="string">"EF_DynamicModelAssembly"</span>,</span><br><span class="line">                    <span class="string">"DynamicModule"</span>,</span><br><span class="line">                    <span class="string">"DynamicType"</span>);</span><br><span class="line"></span><br><span class="line">                <span class="keyword">int</span> fieldCount = dataReader.FieldCount;</span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; fieldCount; i++)</span><br><span class="line">                &#123;</span><br><span class="line">                    Type t = dataReader.GetFieldType(i);</span><br><span class="line">                    <span class="keyword">switch</span> (t.Name.ToLower())</span><br><span class="line">                    &#123;</span><br><span class="line">                        <span class="keyword">case</span> <span class="string">"decimal"</span>:</span><br><span class="line">                            t = <span class="keyword">typeof</span>(Decimal?);</span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">case</span> <span class="string">"double"</span>:</span><br><span class="line">                            t = <span class="keyword">typeof</span>(Double?);</span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">case</span> <span class="string">"datetime"</span>:</span><br><span class="line">                            t = <span class="keyword">typeof</span>(DateTime?);</span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">case</span> <span class="string">"single"</span>:</span><br><span class="line">                            t = <span class="keyword">typeof</span>(<span class="keyword">float</span>?);</span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">case</span> <span class="string">"int16"</span>:</span><br><span class="line">                            t = <span class="keyword">typeof</span>(<span class="keyword">int</span>?);</span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">case</span> <span class="string">"int32"</span>:</span><br><span class="line">                            t = <span class="keyword">typeof</span>(<span class="keyword">int</span>?);</span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">case</span> <span class="string">"int64"</span>:</span><br><span class="line">                            t = <span class="keyword">typeof</span>(<span class="keyword">int</span>?);</span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">default</span>:</span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                    DatabaseExtensions.CreateAutoImplementedProperty(</span><br><span class="line">                        builder,</span><br><span class="line">                        dataReader.GetName(i),</span><br><span class="line">                        t);</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">                <span class="meta">#<span class="meta-keyword">endregion</span></span></span><br><span class="line"></span><br><span class="line">                cmd.Parameters.Clear();</span><br><span class="line">                dataReader.Close();</span><br><span class="line">                dataReader.Dispose();</span><br><span class="line">                cmd.Dispose();</span><br><span class="line">                conn.Close();</span><br><span class="line">                conn.Dispose();</span><br><span class="line"></span><br><span class="line">                Type returnType = builder.CreateType();</span><br><span class="line"></span><br><span class="line">                <span class="keyword">if</span> (parameters != <span class="literal">null</span>)</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">return</span> db.SqlQuery(returnType, sql, parameters);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">else</span></span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">return</span> db.SqlQuery(returnType, sql);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> TypeBuilder <span class="title">CreateTypeBuilder</span>(<span class="params"><span class="keyword">string</span> assemblyName, <span class="keyword">string</span> moduleName, <span class="keyword">string</span> typeName</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            TypeBuilder typeBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(</span><br><span class="line">              <span class="keyword">new</span> AssemblyName(assemblyName),</span><br><span class="line">              AssemblyBuilderAccess.Run).DefineDynamicModule(moduleName).DefineType(typeName,</span><br><span class="line">              TypeAttributes.Public);</span><br><span class="line">            typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);</span><br><span class="line">            <span class="keyword">return</span> typeBuilder;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">CreateAutoImplementedProperty</span>(<span class="params">TypeBuilder builder, <span class="keyword">string</span> propertyName, Type propertyType</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">const</span> <span class="keyword">string</span> PrivateFieldPrefix = <span class="string">"m_"</span>;</span><br><span class="line">            <span class="keyword">const</span> <span class="keyword">string</span> GetterPrefix = <span class="string">"get_"</span>;</span><br><span class="line">            <span class="keyword">const</span> <span class="keyword">string</span> SetterPrefix = <span class="string">"set_"</span>;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// Generate the field.</span></span><br><span class="line">            FieldBuilder fieldBuilder = builder.DefineField(</span><br><span class="line">              <span class="keyword">string</span>.Concat(</span><br><span class="line">                PrivateFieldPrefix, propertyName),</span><br><span class="line">              propertyType,</span><br><span class="line">              FieldAttributes.Private);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// Generate the property</span></span><br><span class="line">            PropertyBuilder propertyBuilder = builder.DefineProperty(</span><br><span class="line">              propertyName,</span><br><span class="line">              System.Reflection.PropertyAttributes.HasDefault,</span><br><span class="line">              propertyType, <span class="literal">null</span>);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// Property getter and setter attributes.</span></span><br><span class="line">            MethodAttributes propertyMethodAttributes = MethodAttributes.Public</span><br><span class="line">              | MethodAttributes.SpecialName</span><br><span class="line">              | MethodAttributes.HideBySig;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// Define the getter method.</span></span><br><span class="line">            MethodBuilder getterMethod = builder.DefineMethod(</span><br><span class="line">                <span class="keyword">string</span>.Concat(</span><br><span class="line">                  GetterPrefix, propertyName),</span><br><span class="line">                propertyMethodAttributes,</span><br><span class="line">                propertyType,</span><br><span class="line">                Type.EmptyTypes);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// Emit the IL code.</span></span><br><span class="line">            <span class="comment">// ldarg.0</span></span><br><span class="line">            <span class="comment">// ldfld,_field</span></span><br><span class="line">            <span class="comment">// ret</span></span><br><span class="line">            ILGenerator getterILCode = getterMethod.GetILGenerator();</span><br><span class="line">            getterILCode.Emit(OpCodes.Ldarg_0);</span><br><span class="line">            getterILCode.Emit(OpCodes.Ldfld, fieldBuilder);</span><br><span class="line">            getterILCode.Emit(OpCodes.Ret);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// Define the setter method.</span></span><br><span class="line">            MethodBuilder setterMethod = builder.DefineMethod(</span><br><span class="line">              <span class="keyword">string</span>.Concat(SetterPrefix, propertyName),</span><br><span class="line">              propertyMethodAttributes,</span><br><span class="line">              <span class="literal">null</span>,</span><br><span class="line">              <span class="keyword">new</span> Type[] &#123; propertyType &#125;);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// Emit the IL code.</span></span><br><span class="line">            <span class="comment">// ldarg.0</span></span><br><span class="line">            <span class="comment">// ldarg.1</span></span><br><span class="line">            <span class="comment">// stfld,_field</span></span><br><span class="line">            <span class="comment">// ret</span></span><br><span class="line">            ILGenerator setterILCode = setterMethod.GetILGenerator();</span><br><span class="line">            setterILCode.Emit(OpCodes.Ldarg_0);</span><br><span class="line">            setterILCode.Emit(OpCodes.Ldarg_1);</span><br><span class="line">            setterILCode.Emit(OpCodes.Stfld, fieldBuilder);</span><br><span class="line">            setterILCode.Emit(OpCodes.Ret);</span><br><span class="line"></span><br><span class="line">            propertyBuilder.SetGetMethod(getterMethod);</span><br><span class="line">            propertyBuilder.SetSetMethod(setterMethod);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">dynamic</span> <span class="title">SqlFunctionForDynamic</span>(<span class="params"><span class="keyword">this</span> Database db, <span class="keyword">string</span> sql, <span class="keyword">params</span> <span class="keyword">object</span>[] parameters</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            IDbConnection conn = DefaultConnection;</span><br><span class="line"></span><br><span class="line">            <span class="comment">//ADO.NET数据库连接字符串</span></span><br><span class="line">            conn.ConnectionString = DefaultConnectionString;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (conn.State != ConnectionState.Open)</span><br><span class="line">            &#123;</span><br><span class="line">                conn.Open();</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            IDbCommand cmd = conn.CreateCommand();</span><br><span class="line">            cmd.CommandText = sql;</span><br><span class="line">            cmd.CommandType = CommandType.StoredProcedure;</span><br><span class="line">            <span class="keyword">if</span> (parameters != <span class="literal">null</span>)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">foreach</span> (<span class="keyword">var</span> item <span class="keyword">in</span> parameters)</span><br><span class="line">                &#123;</span><br><span class="line">                    cmd.Parameters.Add(item);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//1、DataReader查询数据</span></span><br><span class="line">            <span class="keyword">using</span> (IDataReader dataReader = cmd.ExecuteReader())</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">if</span> (!dataReader.Read())</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">//2、DataReader转换Json</span></span><br><span class="line">                <span class="keyword">string</span> jsonstr = Common.JsonHelper.JsonConverter.ToJson(dataReader);</span><br><span class="line">                dataReader.Close();</span><br><span class="line">                dataReader.Dispose();</span><br><span class="line">                cmd.Dispose();</span><br><span class="line">                conn.Close();</span><br><span class="line">                conn.Dispose();</span><br><span class="line">                <span class="comment">//3、Json转换动态类</span></span><br><span class="line">                <span class="keyword">dynamic</span> dyna = Common.JsonHelper.JsonConverter.ConvertJson(jsonstr);</span><br><span class="line">                <span class="keyword">return</span> dyna;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">        <span class="comment"><span class="doctag">///</span> 对可空类型进行判断转换(*要不然会报错)</span></span><br><span class="line">        <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">        <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="value"&gt;</span>DataReader字段的值<span class="doctag">&lt;/param&gt;</span></span></span><br><span class="line">        <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="conversionType"&gt;</span>该字段的类型<span class="doctag">&lt;/param&gt;</span></span></span><br><span class="line">        <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;returns&gt;</span><span class="doctag">&lt;/returns&gt;</span></span></span><br><span class="line">        <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">object</span> <span class="title">CheckType</span>(<span class="params"><span class="keyword">object</span> <span class="keyword">value</span>, Type conversionType</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (conversionType.IsGenericType &amp;&amp; conversionType.GetGenericTypeDefinition().Equals(<span class="keyword">typeof</span>(Nullable&lt;&gt;)))</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">if</span> (<span class="keyword">value</span> == <span class="literal">null</span>)</span><br><span class="line">                    <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">                System.ComponentModel.NullableConverter nullableConverter = <span class="keyword">new</span> System.ComponentModel.NullableConverter(conversionType);</span><br><span class="line">                conversionType = nullableConverter.UnderlyingType;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> Convert.ChangeType(<span class="keyword">value</span>, conversionType);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">        <span class="comment"><span class="doctag">///</span> 判断指定对象是否是有效值</span></span><br><span class="line">        <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">        <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="obj"&gt;</span><span class="doctag">&lt;/param&gt;</span></span></span><br><span class="line">        <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;returns&gt;</span><span class="doctag">&lt;/returns&gt;</span></span></span><br><span class="line">        <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">bool</span> <span class="title">IsNullOrDBNull</span>(<span class="params"><span class="keyword">object</span> obj</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> (obj == <span class="literal">null</span> || (obj <span class="keyword">is</span> DBNull)) ? <span class="literal">true</span> : <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>​</p>
</li>
</ol>

                <!-- <hr> -->
                

                <ul class="pager">
                    
                    <li class="previous">
                        <a href="/2017/02/15/mvc-lesson2/" data-toggle="tooltip" data-placement="top"
                           title="Asp.net MVC系列教程2——创建数据库和数据模型">&larr; Previous Post</a>
                    </li>
                    
                    
                    <li class="next">
                        <a href="/2017/02/16/mvc-lesson4/" data-toggle="tooltip" data-placement="top"
                           title="Asp.net MVC系列教程4——Spring IOC、DI">Next Post &rarr;</a>
                    </li>
                    
                </ul>

                <!-- social-share start -->
                <div class="social-share" style="padding-top:20px;text-align:center;"></div>
                <!--  css & js -->
                <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/css/share.min.css
                ">
                <script src="https://cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/js/social-share.min.js"></script>
                <!-- social-share end -->


                

                
                <!-- disqus 评论框 start -->
                <div class="comment">
                    <div id="disqus_thread" class="disqus-thread"></div>
                </div>
                <!-- disqus 评论框 end -->
                

            </div>

            <div class="hidden-xs col-sm-3 toc-col">
                <div class="toc-wrap">
                    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#简述"><span class="toc-text">简述</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#索引"><span class="toc-text">索引</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#项目开始"><span class="toc-text">项目开始</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#一、Common类库添加"><span class="toc-text">一、Common类库添加</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#二、Service项目添加IRepository，RepositoryBase，DatabaseExtensions"><span class="toc-text">二、Service项目添加IRepository，RepositoryBase，DatabaseExtensions</span></a></li></ol></li></ol></li></ol>
                </div>
            </div>
        </div>

        <div class="row">
            <!-- Sidebar Container -->

            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                sidebar-container">

                <!-- Featured Tags -->
                
                <section>
                    <!-- no hr -->
                    <h5 class="text-center">
                        <a href="/tags/">FEATURED TAGS</a>
                    </h5>
                    <div class="tags">
                        
                        <a class="tag" href="/tags/#Asp.net"
                           title="Asp.net">Asp.net</a>
                        
                        <a class="tag" href="/tags/#MVC"
                           title="MVC">MVC</a>
                        
                        <a class="tag" href="/tags/#Repository"
                           title="Repository">Repository</a>
                        
                        <a class="tag" href="/tags/#EntityFramework"
                           title="EntityFramework">EntityFramework</a>
                        
                    </div>
                </section>
                

                <!-- Friends Blog -->
                
            </div>
        </div>

    </div>
</article>

<div id="music">
    <audio id="bell" src="/music/Faded.mp3" preload="auto" loop autoplay></audio>
</div>
<!-- <embed style="position:fixed;right:0;bottom:0;"; src="http://www.xiami.com/widget/274513503_1776204257/singlePlayer.swf" type="application/x-shockwave-flash" width="257" height="33" wmode="transparent"></embed> -->
<!-- <embed style="position:fixed;right:0;bottom:-44px;"; src="http://www.xiami.com/widget/274513503_1775988866,1774426379,1770760772,1772121439,3567313,_235_246_FF8719_494949/multiPlayer.swf" allowscriptaccess="always" quality="high" wmode="opaque" menu="false" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="235" height="246" title="Adobe Flash Player"> -->
<!-- music control -->
<script src="/js/music.js"></script>

<div class="gotoTop"></div>
<script src="/js/gotoTop.js"></script>



<!-- disqus 公共JS代码 start (一个网页只需插入一次) -->
<script type="text/javascript">
    /* * * CONFIGURATION VARIABLES * * */
    var disqus_shortname = "guanghuiwang";
    var disqus_identifier = "https://wghglory.github.io/2017/02/16/mvc-lesson3/";
    var disqus_url = "https://wghglory.github.io/2017/02/16/mvc-lesson3/";

    (function () {
        var dsq = document.createElement('script');
        dsq.type = 'text/javascript';
        dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
</script>
<!-- disqus 公共JS代码 end -->



<!-- Footer -->
<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1 text-center">
                <br>
                <ul class="list-inline text-center">
                
                
                

                

                
                <li>
                    <a target="_blank" href="https://www.jianshu.com/u/85d2cacc9fcd">
                                <span class="fa-stack fa-lg">
                                    <i class="fa fa-circle fa-stack-2x"></i>
                                    <i class="fa fa-stack-1x fa-inverse">简</i>
                                </span>
                    </a>
                </li>
                

                
                    <li>
                        <a target="_blank" href="https://www.facebook.com/wghglory">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-facebook fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
                

                
                    <li>
                        <a target="_blank"  href="https://github.com/wghglory">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
                

                
                    <li>
                        <a target="_blank"  href="https://www.linkedin.com/in/guanghuiwang">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-linkedin fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
                

                </ul>
                <p class="copyright text-muted">
                    Copyright &copy; Guanghui 2017
                    <br>
                    <span id="busuanzi_container_site_pv" style="font-size: 12px;">PV: <span id="busuanzi_value_site_pv"></span> Times</span>
                    <br>
                    Modified theme based on <a href="https://haojen.github.io/">Haojen Ma</a>' Anisina
                </p>

            </div>
        </div>
    </div>
</footer>

<!-- jQuery -->
<script src="/js/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="/js/bootstrap.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src="/js/blog.js"></script>

<!-- code block fold/unfold button and copy button -->
<script src="/js/codeBlockEnhance.js"></script>

<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>

<!-- jquery.tagcloud.js -->
<script>
    // only load tagcloud.js in tag.html
    if($('#tag_cloud').length !== 0){
        async("https://wghglory.github.io/js/jquery.tagcloud.js",function(){
            $.fn.tagcloud.defaults = {
                //size: {start: 1, end: 1, unit: 'em'},
                color: {start: '#bbbbee', end: '#0085a1'},
            };
            $('#tag_cloud a').tagcloud();
        })
    }
</script>

<!--fastClick.js -->
<script>
    async("//cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js", function(){
        var $nav = document.querySelector("nav");
        if($nav) FastClick.attach($nav);
    })
</script>

<!-- Google Analytics -->


<script>
    // dynamic User by Hux
    var _gaId = 'UA-91771069-1';
    var _gaDomain = 'wghglory.github.io';
    // Originial
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
                (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    ga('create', _gaId, _gaDomain);
    ga('send', 'pageview');
</script>


<!-- Baidu Tongji -->


<!-- swiftype -->
<script type="text/javascript">
  // (function(w,d,t,u,n,s,e){w['SwiftypeObject']=n;w[n]=w[n]||function(){
  // (w[n].q=w[n].q||[]).push(arguments);};s=d.createElement(t);
  // e=d.getElementsByTagName(t)[0];s.async=1;s.src=u;e.parentNode.insertBefore(s,e);
  // })(window,document,'script','//s.swiftypecdn.com/install/v2/st.js','_st');
  //
  // _st('install','undefined','2.0.0');
</script>

<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<!--wechat title img-->
<img class="wechat-title-img" src="/images/avatar.jpg">
</body>

</html>
